#region Copyright 2006 Robert McLaws. Licensed under Creative Commons.

// This code is the original work of Robert McLaws. You may modify the code
// to suit your purposes, but please leave this header here. Thank you.
//
// Author: Robert W. McLaws
// Version: 1.0.60822
// Last Updated: 22 August 2006, 2:22 PM
// Updates: http://www.robertmclaws.com/files/folders/cshandlers/entry106.aspx

#endregion

#region Import Declarations

using System.IO;
using System.Text;
using System.Web;
using CommunityServer.Configuration;
using CommunityServer.Components;
using CommunityServerStuff.Utilities;
using CommunityServerStuff.Writers;

#endregion

namespace CommunityServerStuff.Handlers
{
    /// <summary>
    /// 
    /// </summary>
    public class OpenSearchHandler : IHttpHandler
    {

        #region Private Members

        protected HttpContext _context;

        #endregion

        #region Properties

        #region IsReusable

        /// <summary>
        /// 
        /// </summary>
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        #endregion

        #region IfModifiedSinceHeader

        /// <summary>
        /// 
        /// </summary>
        protected string IfModifiedSinceHeader
        {
            get
            {
                return _context.Request.Headers["If-Modified-Since"];
            }
        }

        #endregion

        #region IfNoneMatchHeader

        protected string IfNoneMatchHeader
        {
            get
            {
                return _context.Request.Headers["If-None-Match"];
            }
        }

        #endregion

        #endregion

        #region Methods

        #region WriteNotModifiedResponse

        /// <summary>
        /// 
        /// </summary>
        private void WriteNotModifiedResponse()
        {
            _context.Response.StatusCode = 304;
            _context.Response.Status = "304 Not Modified";
        }

        #endregion

        #region ProcessRequest

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentEncoding = Encoding.UTF8;
            context.Response.ContentType = "text/xml";
            context.Response.Cache.SetCacheability(HttpCacheability.Public);
            OpenSearchWriter doc = new OpenSearchWriter(CSContext.Current.SiteSettings, new EncodedStringWriter(new StringBuilder(), Encoding.UTF8));
            context.Response.Write(doc.ToString());
        }

        #endregion

        #endregion

    }
}